package com.otherTest.date;

import com.otherTest.other.DateUtil;
import com.otherTest.other.StringUtil;
import org.junit.Test;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author Conceit
 * @version 1.0, 2016/12/4
 */

public class TestDate {
    public static String getFormatTime(Date date, String pattern) {
        if (StringUtil.isEmpty(pattern))
            pattern = "yyyy-MM-dd HH:mm:ss";
        if (date == null)
            date = new Date();
        DateFormat format = new SimpleDateFormat(pattern);
        return format.format(date);
    }

    @Test
    public void convertHours() {

        //将时间转化为String
        Date beforeDate = new Date();
        String beforeDateString = DateUtil.getFormatTime(beforeDate, "yyyy-MM-dd HH:mm:ss");
        System.out.println(beforeDateString + "beforeDateString");
        //修改时间
        Date convertDate = null;
        Date afterTime = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            convertDate = sdf.parse(beforeDateString);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(convertDate);
            calendar.add(Calendar.HOUR_OF_DAY, 8);
            System.out.println(calendar.getTime());
            System.out.println(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //System.out.println( afterTime+"修改后的结果！！！");

    }

    public void test2() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        try {
            date = sdf.parse("2012-07-25 21:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar ca = Calendar.getInstance();
        ca.setTime(date);
        ca.add(Calendar.HOUR_OF_DAY, 3);
        System.out.println(sdf.format(ca.getTime()));
    }
}
